* set path

	global raw "~/agriculture_temperature"
	
	global temp "~/revision_codes/temp"
	global visual "~/revision_codes/visual"
	
	use $raw/data/merged_for_reg, clear

	xtset ctycode year
	
********************************************************************************
* Temperature distributions
********************************************************************************	
		
	sum dd5binNeg dd5bin? dd5bin??	if ln_aoutput!=. & inlist(region3,1,2)
	sum dd5binNeg dd5bin? dd5bin??	if ln_aoutput!=. & region3==1
	sum dd5binNeg dd5bin? dd5bin??	if ln_aoutput!=. & region3==2
	
	sum dd3bin? dd3bin??	if ln_aoutput!=. & inlist(region3,1,2)
	sum dd3bin*	if ln_aoutput!=. & region3==1
	sum dd3bin*	if ln_aoutput!=. & region3==2	
	
********************************************************************************
* Contemporaneous Value Estimates(Figure 3 ,Table S5-S7)
********************************************************************************

* set reference bin

	rename dd5bin10 REF_BIN
	
	tempfile r1 r2 r3 
	
	* ag values - overall
	******all counties*****
	foreach t in 5 {
		foreach x in aoutput farming_output forest_output livestock_output fishery_output{
			
			qui reghdfe ln_`x' dd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & inlist(region3,1,2), ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store all_`x'_`t'C
		parmest , label saving(`"`r1'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("All areas")
		*******north*****		
			qui reghdfe ln_`x' dd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==1, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store north_`x'_`t'C			
		parmest , label saving(`"`r2'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("North")
		******south*****	
			qui reghdfe ln_`x' dd`t'bin* ///
					c.pre##c.pre c.ssd##c.ssd c.rhu##c.rhu c.prs##c.prs c.win##c.win ///
					i.pro#c.year##c.year ///
					if inrange(year,1995,2015) & region3==2, ///
					absorb(ctycode year) cluster(ctycode proXyear) 
			estimates store south_`x'_`t'C	
		parmest , label saving(`"`r3'"',replace) format(p %8.2f) stars(0.1 0.05 0.01)  idstr("South")	
			
			preserve 
			use `"`r1'"',clear
			append using `"`r2'"' `"`r3'"' 
			gen sector = "`x'"
			rename id  spec
			save $temp/est_RevAgVal_`x',replace
			restore			
			
			}
		}
		
	********************************************************
	*************** arrange the coefficient ****************
	********************************************************
	use $temp/est_RevAgVal_aoutput,clear
	
	foreach file in farming_output forest_output livestock_output fishery_output {
		append using $temp/est_RevAgVal_`file'
	}
	
	// keep temp variables only
	keep if strmatch(parm,"*dd5bin*")
	
	//level varibables
	gen level = -2.5 if strmatch(parm, "dd5binNeg")
	replace level = 2.5 if strmatch(parm, "dd5bin0")
	replace level = 7.5 if strmatch(parm, "dd5bin5")
	replace level = 17.5 if strmatch(parm, "dd5bin15")
	replace level = 22.5 if strmatch(parm, "dd5bin20")
	replace level = 27.5 if strmatch(parm, "dd5bin25")
	replace level = 32.5 if strmatch(parm, "dd5bin30")
	replace level = 37.5 if strmatch(parm, "dd5bin35")

	
	// sector name 
	replace sector = "Aggregate" if strmatch(sector,"aoutput")
	replace sector = "Cropping" if strmatch(sector,"farming_output")
	replace sector = "Livestock" if strmatch(sector,"livestock_output")
	replace sector = "Forestry" if strmatch(sector,"forest_output")
	replace sector = "Fishery" if strmatch(sector,"fishery_output")
		
	rename estimate coef 
	rename stderr se 
	rename min95 ci_l
	rename max95 ci_h
	
	// add zero-value lines
	bysort spec sector : gen last = _n == _N
	expand 2 if last
	sort spec sector parm
	replace level = 12.5 if parm==parm[_n-1]
	replace coef = 0 if parm==parm[_n-1]
	replace se = 0 if parm==parm[_n-1]
	replace ci_h = 0 if parm==parm[_n-1]
	replace ci_l = 0 if parm==parm[_n-1]
	
	save $visual/visual_RevAgVal,replace
	
	// delete dta data 
	cd $temp/
	fs *.dta
	foreach f in `r(files)'{
	   erase "`f'"
	}
	
	
